package io.jt808.proto.item;


import io.norland.buffer.AutoBuffer;
import io.norland.proto.IProto;
import lombok.Data;

@Data
public class PositionAdditionalItem implements IProto {
    private byte additionalId;
    private byte additionLength;
    private IProto detail;

    public byte[] writeToBytes() {
        AutoBuffer buffer = new AutoBuffer();
        buffer.writeByte(additionalId);
        buffer.writeByte(additionLength);
        if (detail != null)
            buffer.writeBytes((detail).writeToBytes());
        return buffer.array();
    }

    public void readFromBuf(AutoBuffer buffer) {
        additionalId = buffer.readByte();
        additionLength = buffer.readByte();
        detail = PositionAdditionalFactory.create(additionalId, buffer.slice(buffer.readerIndex(), additionLength));
        buffer.skipBytes(additionLength);
        if (this.detail == null)
            throw new IllegalArgumentException("参数不对");
    }
}